package com.awcall.iot.service.impl;

import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.Role;
import com.awcall.iot.mapper.RoleMapper;
import com.awcall.iot.service.IRoleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wuhui
 * @since 2018-03-05
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public ResponseResult validate(Role role) {
        Wrapper<Role> wrapper = new EntityWrapper<Role>()
                .eq(Role.ROLE_NAME, role.getRoleName())
                .last("limit 1");
        if (null != role.getId()) {
            wrapper.ne(Role.ID, role.getId());
        }

        Role role1 = selectOne(wrapper);
        if (role1 == null) {
            return null;
        }

        return new ResponseResult<>(1, "权限名称已存在");
    }

    @Override
    public ResponseResult insertRole(Role role) {
        ResponseResult responseResult = validate(role);
        if (responseResult != null) {
            return responseResult;
        }
        insert(role);
        return new ResponseResult();
    }

    @Override
    public ResponseResult updateRole(Role role) {
        ResponseResult responseResult = validate(role);
        if (responseResult != null) {
            return responseResult;
        }
        updateById(role);
        return new ResponseResult();
    }

    @Override
    public ResponseResult findByAdminUserId(Long id) {
        List<Role> roleList = baseMapper.findByAdminUserId(id);
        return new ResponseResult<>(roleList);
    }

}
